//
//  LCVisitorBaseController.swift
//  SinaWeiBo
//
//  Created by mac on 16/12/1.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit

class LCVisitorBaseController: UITableViewController {
    //1定义一个属性来判断用户是否登录

    var isLogon:Bool = LCOAuthViewModel.sharedViewModel.accessToken != nil
    
    //2将访客视图定义全局变量
    var visitorView: LCVisitorView?
    
    //第一全局的webviewvc
    var oauthVC: LCOAuthController?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
    }
    private func setNavigationItem(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: #selector(registerButtonAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", target: self, action: #selector(lagionButtonAction))
        
    }
    //注册
    func registerButtonAction(){
 
        presentWebVc()
        
    }
    //登录
    func lagionButtonAction(){

        presentWebVc()
    }
    
    //MARK:跳转控制器
    func presentWebVc(){
        oauthVC = LCOAuthController()
        let naviVc = LCNavigationController(rootViewController: oauthVC!)
        //MARK:问题1为什么不直接push
//        navigationController?.pushViewController(oauthVC, animated: true)
        
        present(naviVc, animated: false, completion: nil)
    }
    
    
    //添加一个button来从webviewVC返回主页面
    func dismissVC(){
        oauthVC?.dismiss(animated: true, completion: nil)
    }
    

    //重写loadView方法
    override func loadView() {
        //判断用户是否登录,如果没有登录，将根view替换成访客view，如果已经登录，调用super的loadview方法，正常现实
        if isLogon == false {
            visitorView = LCVisitorView()
            
            view = visitorView
            
            visitorView?.callBack = {
                self.presentWebVc()
            }
            
            //现实navigationItem的注册和登录button
            setNavigationItem()
        }else{
            super.loadView()
        }
        
    }
    
    
    


}
